package com.kila.test;

import com.kila.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.List;
import java.io.Reader;

public class test {


    //查询单个学生
    public static void queryStudent(){

        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();

            Student s =(Student) session.selectOne("com.kila.entity.studentMapper.queryStudentByStuno",1);

            s.toString();



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }
    }

    //查询全部学生
    public static void queryAllStudent(){
        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();

            List<Student> students = session.selectList("com.kila.entity.studentMapper.queryAllStudents");

          System.out.println(students);



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }



    }

   //增加一个学生
     public static void addStudent(){
        Student stu = new Student();
        stu.setStuNo(3);
        stu.setStuName("王五");
        stu.setStuAge(17);
        stu.setGranName("3band");

        SqlSession session=null;

         try {
             //加载MyBatis连接配置文件
             Reader reader= Resources.getResourceAsReader("config.xml");
             //通过SqlSessionFactoryBuilder.build()方法获得对象
             SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

             //获取session
             session = sessionFactory.openSession();

             session.insert("com.kila.entity.studentMapper."+"addStudent",stu);
            //新增加数据需要提交事务
             session.commit();
             System.out.println("增加了一行数据");



         }catch (Exception e){
             e.printStackTrace();
         }finally {
             session.close();

         }


     }


     //删除学生

    public static void deleteStudentByStuno(){


        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();

           int count = session.delete("com.kila.entity.studentMapper."+"deleteStudentBystuno",3);
            //新增加数据需要提交事务
            session.commit();
            System.out.println("删除了"+count+"行数据");



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }


    }

     //修改学生
    public static void updateStudentByStuno(){


        SqlSession session=null;
        Student stu  = new Student(2,"zl",14,"g3");

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();

            int count = session.update("com.kila.entity.studentMapper."+"updateStudentByno",stu);
            //新增加数据需要提交事务
            session.commit();
            System.out.println("修改了"+count+"行数据");



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }



    }




    public static void main(String[] args){
       //  addStudent();
       // queryAllStudent();
        // deleteStudentByStuno();
        // queryAllStudent();
      //   updateStudentByStuno();
         queryAllStudent();
    }
}
